/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kurvykurvy.actions;

import com.kurvykurvy.core.ui.ImageStripTopComponent;
import com.kurvykurvy.image.ImageLoader;
import com.kurvykurvy.ui.filter.JPEGFileFilter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.ErrorManager;
import javax.swing.JFileChooser;
import org.openide.util.Exceptions;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.CallableSystemAction;
import org.openide.windows.TopComponent;

public final class LoadImagesAction 
        extends CallableSystemAction
        implements ActionListener
{

    public void actionPerformed( ActionEvent e )
    {
        // TODO implement action body
        performAction();
    }

    @Override
    public void performAction()
    {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
        fileChooser.setMultiSelectionEnabled( true );
        fileChooser.setFileFilter( new JPEGFileFilter() );

        int options = fileChooser.showDialog(
                TopComponent.getRegistry().getActivated(),
                NbBundle.getMessage( LoadImagesAction.class, "LoadImagesAction_load_label" )
        );

        if( JFileChooser.APPROVE_OPTION == options )
        {
            File[] files = fileChooser.getSelectedFiles();
            for( int i = 0; i < files.length; i++ )
            {
                ImageLoader imageLoader = ImageLoader.getInstance();
                try
                {
                    imageLoader.loadImage( files[i] );
                }
                catch ( IOException ex )
                {
                    Exceptions.printStackTrace( ex );
                }
            }
        }

        if( !ImageStripTopComponent.getDefault().isOpened() )
        {
            ImageStripTopComponent.getDefault().open();
        }
    }

    @Override
    public String getName()
    {
        return NbBundle.getMessage( LoadImagesAction.class, "LoadImage_action_lbl" );
    }

    @Override
    public HelpCtx getHelpCtx()
    {
        return null;
    }
}
